﻿using RpcCentral.Collect.Model;
using RpcCentral.Common;
using WeDonekRpc.Helper;
using WeDonekRpc.Model;
namespace RpcCentral.Collect.Controller
{
    public class TransmitConfigController : DataSyncClass
    {
        public TransmitConfigController(long systemType, long rpcMerId)
        {
            this._RpcMerId = rpcMerId;
            this._SystemType = systemType;
        }
        private readonly long _RpcMerId;
        private readonly long _SystemType;
        private string _Ver = string.Empty;
        private volatile int _VerNum = 0;
        private Transmit[] _Transmit;

        public int VerNum { get => this._VerNum;  }

        protected override void SyncData()
        {
            using (IocScope scope = UnityHelper.CreateScore())
            {
                ITransmitHelper helper = scope.Resolve<ITransmitHelper>();
                if (helper.GetTransmits(this._RpcMerId, this._SystemType, ref this._Ver, out this._Transmit))
                {
                    this._VerNum = Tools.GetRandom();
                }
            }
        }
        public bool CheckVer(ref int ver)
        {
            if (ver != this._VerNum)
            {
                ver = this._VerNum;
                return true;
            }
            return false;
        }
        public void InitConfig(ServerConfig config)
        {
            ServerTransmit[] list = this._Transmit.ConvertMap<Transmit, ServerTransmit>(a => a.ServerId == config.ServerId);
            if (list.Length != 0)
            {
                config.Transmit = list;
                config.IsTransmit = true;
            }
        }
        public ServerTransmit[] GetTransmits(long serverId)
        {
            return this._Transmit.ConvertMap<Transmit, ServerTransmit>(a => a.ServerId == serverId);
        }
    }
}
